package bill.study.javaPuzzlers.classy;

/**
 * Static fields are shared by their declaring class and any subclasses. If you
 * need a separate copy of a field for each subclass, you must declare a
 * separate static field in each subclass. If you need a separate copy for each
 * instance, declare a nonstatic field in the base class. Also, favor
 * composition over inheritance unless the derived class really is a kind of the
 * base class.
 * 
 * @author yangliu
 * 
 */
class Counter {
	private static int count;

	public static void increment() {
		count++;
	}

	public static int getCount() {
		return count;
	}
}

class Dog extends Counter {
	public Dog() {
	}

	public void woof() {
		increment();
	}
}

class Cat extends Counter {
	public Cat() {
	}

	public void meow() {
		increment();
	}
}

public class Ruckus {
	public static void main(String[] Arg) {
		Dog dogs[] = { new Dog(), new Dog() };
		for (int i = 0; i < dogs.length; i++)
			dogs[i].woof();

		Cat cats[] = { new Cat(), new Cat(), new Cat() };
		for (int i = 0; i < cats.length; i++) {
			cats[i].meow();
		}

		System.out.print(Dog.getCount() + "  woofs and ");
		System.out.println(Cat.getCount() + " meows.");
	}
}